﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using jmail;

using Code.BLL;
using Code.Models;

namespace Code.Commen
{
    public class Jmail
    {
        public bool Send(string touser,string title,string body,bool ishtml=false)
        {
            var rs = false;
            var option=CacheManage.OptionSite;

            if (string.IsNullOrEmpty(touser))
                throw new ValidateException("收件地址不能为空");
            if (!option[Option_Site_Key.Site_MailServer_Enable].ToBool(false))
                throw new AppException("系统未启用邮件发送功能");

            var mail = new jmail.MessageClass();
            mail.Charset = "gb2312";
            mail.ISOEncodeHeaders = false;

            var from = option[Option_Site_Key.Site_Info_Email].Value;
            var name = option[Option_Site_Key.Site_Info_Company].Value;
            var uid = option[Option_Site_Key.Site_MailServer_UserName].Value;
            var pwd = option[Option_Site_Key.Site_MailServer_Password].Value;
            var server = option[Option_Site_Key.Site_MailServer_ServerAddr].Value;
            var port = option[Option_Site_Key.Site_MailServer_Port].ToInt32(25);
            if (string.IsNullOrEmpty(server))
                throw new AppException("邮件服务器地址未设置");
            if (string.IsNullOrEmpty(uid))
                throw new AppException("邮件服务器用户名未设置");
            if (string.IsNullOrEmpty(pwd))
                throw new AppException("邮件服务器登录密码未设置");

            mail.From = uid;
            mail.Subject = title;
            mail.MailServerUserName = uid;
            try
            {
                var key=Salt.Password.GetDesSalt();
                DES des = new DES();
                mail.MailServerPassWord = des.DecryptString(pwd,key.Key, key.Value);
            }
            catch { throw new AppException("邮件服务器登录密码读取错误"); }
            mail.Priority = 3;
            mail.AddRecipient(touser);
            if (ishtml)
                mail.HTMLBody = body;
            else
                mail.Body = body;
            rs=mail.Send(string.Format("{0}:{1}",server,port));
            mail.Close();

            return rs;
        }
    }

    /*
    Set JMail = Server.CreateObject("JMail.Message") 
    JMail.Charset = "gb2312" ' 邮件字符集，默认为"US-ASCII"
    ' JMail.ISOEncodeHeaders = False ' 是否进行ISO编码，默认为True

    ' 发送者信息（可用变量方式赋值）
    JMail.From = "Licnse@gmail.com" ' 发送者地址
    JMail.FromName = "冷雨夜" ' 发送者姓名
    JMail.Subject = "你好吗？" ' 邮件主题

    ' 身份验证
    JMail.MailServerUserName = "myusername" ' 身份验证的用户名
    JMail.MailServerPassword = "mypassword" ' 身份验证的密码

    ' 设置优先级，范围从1到5，越大的优先级越高，3为普通
    JMail.Priority = 3

    JMail.AddHeader "Originating-IP", Request.ServerVariables("REMOTE_ADDR")

    ' 加入一个收件人【变量email：收件人地址】可以同一语句重复加入多个
    JMail.AddRecipient("someone@somehost.com")

    ' 加入附件【变量filename：附件文件的绝对地址，确保用户IUSR_机器名有访问的权限】
    ' 【参数设置是(True)否(False)为Inline方式】
    'contentId = JMail.AddAttachment (Server.MapPath("jmail.asp"), True)

    ' 邮件主体（HTML(注意信件内链接附件的方式)）
    JMail.HTMLBody = "<html><head><META content=zh-cn http-equiv=Content-Language><meta http-equiv=""Content-Type"" content=""text/html; charset=gb2312""><style type=text/css>A:link { FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #000000}A:visited {FONT-SIZE: 9pt; TEXT-DECORATION: none; color: #666666}A:hover {COLOR: #ff6600; FONT-SIZE: 9pt; TEXT-DECORATION: underline}BODY {FONT-SIZE: 9pt} --></style></head><body><font color=red>邮件正文</font><br><font color=green>邮件正文</font><br><b>邮件正文</b></body></html>"

    ' 邮件主体（文本部分）
    JMail.Body = "我们的邮件采用了HTML格式，但是您的邮件查看软件可能不支持。。。邮件正文,邮件正文,邮件正文"

    ' 发送【调用格式：objJMail.Send([username:password@]SMTPServerAddress[:Port])】
    JMail.Send("smtp.163.com")

    ' 关闭并清除对象
    JMail.Close()
    Set JMail = Nothing

    */

}
